function isChinaName(name) {
    var pattern = /^[\u4E00-\u9FA5]{1,6}$/;
    return pattern.test(name);
}

/**
 得到地址栏的参数
 */
function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

function returnUser() {
    let users = window.localStorage.getItem("users");
    let data = JSON.parse(users);
    return data;
}

/**
 * 语音播放文本
 * @param text 信息
 */
function playMessage (text){
    var url = "http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&text=" + encodeURI(text);// baidu文字转语音
    var voiceContent = new Audio(url);
    voiceContent.src = url;
    voiceContent.play();
}


// 验证身份证
function isCardNo(card) {
    var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return pattern.test(card);
}


// 验证手机号
function isPhoneNo(phone) {
    var pattern = /^1[34578]\d{9}$/;
    return pattern.test(phone);
}

function notNull(inputid, spanid) {
    $(inputid).blur(function() {
        if ($.trim($(inputid).val()).length == 0) {
            $(spanid).html("<font size='3px' color='red'>× 请完善信息</font>");
        }
    });
    $(inputid).focus(function() {
        $(spanid).html("");
    });
};
/*用户名判断*/
function userName(inputid, spanid) {
    $(inputid).blur(function() {
        if ($.trim($(inputid).val()).length == 0) {
            $(spanid).html("<font size='3px' color='red'>× 名称没有输入</font>");
        } else {
            if (isChinaName($.trim($(inputid).val())) == false) {
                $(spanid).html("<font size='3px' color='red'>× 名称不合法</font>");
            }
        }
    });
    $(inputid).focus(function() {
        $(spanid).html("");
    });


};
/*身份证判断*/
function userID(inputid, spanid) {
    $(inputid).blur(function() {
        if ($.trim($(inputid).val()).length == 0) {
            $(spanid).html("<font size='3px' color='red'>× 身份证号码没有输入</font>");
        } else {
            if (isCardNo($.trim($(inputid).val())) == false) {
                $(spanid).html("<font size='3px' color='red'>× 身份证号不正确</font>");
            }
        }
    });
    $(inputid).focus(function() {
        $(spanid).html("");
    });
};


/*手机号判断*/
function userTel(inputid, spanid) {
    $(inputid).blur(function() {
        if ($.trim($(inputid).val()).length == 0) {
            $(spanid).html("<font size='3px' color='red'>× 手机号没有输入</font>");
        } else {
            if (isPhoneNo($.trim($(inputid).val())) == false) {
                $(spanid).html("<font size='3px' color='red'>× 手机号码不正确</font>");
            }
        }
        $(inputid).focus(function() {
            $(spanid).html("");
        });
    });
};
/*userTel('#fsPhone', "#falsePhone");
userID('#identity', "#checkExistID");
userName('#name', "#checkExistname");*/


/**
 * 得到项目的根目录
 *
 * @returns
 */
function getRootPath() {
    // 获取当前网址，如?? http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath = window.document.location.href;
    // 获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    // 获取主机地址，如?? http://localhost:8083
    var localhostPaht = curWwwPath.substring(0, pos);
    // 获取??"/"的项目名，如??/uimcardprj
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    //return (localhostPaht + projectName)+"/";
    return localhostPaht+"/";
}
/**
 得到地址栏的参数
 */
function getQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

/**
 * 判断是否为总公司
 * @returns {string|boolean}
 */
function whetherZGS() {
    let users = window.localStorage.getItem("users");
    let data = JSON.parse(users);
    if(data!=null || data!=''){
        return data.companyId=='0' ;
    }else{
        return "网络错误,请重新登入";
    }

}

function  authorization (str) {
    let item = window.localStorage.getItem("moduleList");
    //console.info(JSON.parse(item))

    return JSON.parse(item).filter(item => str == item.perms).length > 0;

}

Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1, // month
        "d+": this.getDate(), // day
        "h+": this.getHours(), // hour
        "m+": this.getMinutes(), // minute
        "s+": this.getSeconds(), // second
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter
        "S": this.getMilliseconds() // millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}

/**
 * 将数字时间转换为正确的时间格??
 * @param time
 * @param patten
 * @returns
 */
function formatDate(time,patten){
    var date = new Date(time).format(patten);
    return date ;
}

/**
 * 表单自动赋???
 */
(function ($) {
    $.fn.extend({
        initForm: function (options) {
            //默认参数
            var defaults = {
                formdata: "",
                isDebug: true //是否??要调试，这个用于??发阶段，发布阶段请将设置为false，默认为false,true将会把name value打印出来
            }
            //如果传入的json字符串，将转为json对象
            var tempData = "";
            if ($.type(options) === "string") {
                defaults.formdata = JSON.parse(options);
            } else {
                defaults.formdata = options;
            }
            //设置参数
            // var setting = $.extend({}, defaults, tempData);
            var setting = defaults;
            var form = this;
            formdata = setting.formdata;

            //如果传入的json对象为空，则不做任何操作
            if (!$.isEmptyObject(formdata)) {
                var debugInfo = "";
                $.each(formdata, function (key, value) {
                    //是否??启调试，??启将会把name value打印出来
                    if (setting.isDebug) {
                        debugInfo += "name:" + key + "; value:" + value + "\r\n ";
                    }
                    //表单处理
                    var formField = form.find("[name='" + key + "']");
                    if ($.type(formField[0]) === "undefined") {
                        if (setting.isDebug) {
                            console.warn("can not find name:[" + key + "] in form!!!"); //没找到指定name的表??
                        }
                    } else {
                        var fieldTagName = formField[0].tagName.toLowerCase();
                        if (fieldTagName == "input") {
                            if (formField.attr("type") == "radio") {
                                $("input:radio[name='" + key + "'][value='" + value + "']").attr("checked", "checked");
                            } else if (formField.attr("type") == "checkbox") {
                                $("input:checkbox[name='" + key + "'][value='" + value + "']").attr("checked", "checked");
                            } else {
                                formField.val(value);
                            }
                        } else if (fieldTagName == "label") {
                            formField.html(value);
                        } else {
                            formField.val(value);
                        }
                    }
                    //图片链接处理form.find("img[fieldata=img_url]")
                    var formImage = form.find("img[fieldata=" + key + "]");
                    if ($.type(formImage[0]) != "undefined") {
                        formImage.attr("src", value);
                    }
                    //a链接处理
                    var formLink = form.find("a[fieldata=" + key + "]");
                    if ($.type(formLink[0]) != "undefined") {
                        formLink.attr("href", value);
                    }
                })
                if (setting.isDebug) {
                    console.log(debugInfo);
                }
            }
            return form; //返回对象，提供链式操??
        }
    });
})